/**
 * @UnitDescriptior.java  COPYRIGHT xiaojian
 * 
 * Created on 2017年10月27日 下午3:26:54
 */
package xiaojian.toolkit.ebus.unit.yaml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;

import xiaojian.toolkit.ebus.unit.UnitSource;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
public class YamlUnitSource implements UnitSource {

	private static final Logger logger = LoggerFactory.getLogger(YamlUnitSource.class);
	
	private long lastModified = System.currentTimeMillis();

	private String name;
	private List<String> parameters;
	private String template;
	private List<String> depends;

	public String getTemplate() {
		return template;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public List<String> getParameters() {
		return parameters;
	}

	public void setParameters(List<String> parameters) {
		this.parameters = parameters;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public List<String> getDepends() {
		return depends;
	}

	public void setDepends(List<String> depends) {
		this.depends = depends;
	}

	public Properties getExtendsProperties() {
		final Properties properties = new Properties();

		if (parameters == null || parameters.size() == 0) {
			return properties;
		}

		String strParam = StringUtils.join(parameters, StringUtils.CR);

		try {
			InputStream is = new ByteArrayInputStream(strParam.getBytes(Charsets.UTF_8));
			properties.load(is);
		} catch (IOException e) {
			logger.warn("exception when load properties, detail: {}", e);
		}
		return properties;
	}
	
    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(parameters.toArray(new String[parameters.size()]));
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result
                + ((parameters == null) ? 0 : parameters.hashCode());
        return result;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        YamlUnitSource other = (YamlUnitSource) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (parameters == null) {
            if (other.parameters != null)
                return false;
        } else if (!parameters.equals(other.parameters))
            return false;
        return true;
    }
    
	/**
	 * @see xiaojian.toolkit.ebus.unit.UnitSource#getInputStream()
	 */
	@Override
	public InputStream getInputStream() {
		return null;
	}
	
	/**
	 * @see xiaojian.toolkit.ebus.unit.UnitSource#lastModified()
	 */
	@Override
	public long lastModified() {
		return lastModified;
	}
	
	public void updateLastModified() {
		this.lastModified = System.currentTimeMillis();
	}
	
	public void setLastModified(long update) {
		this.lastModified = update;
	}
	
	/**
	 * @see xiaojian.toolkit.ebus.unit.UnitSource#getType()
	 */
	@Override
	public String getType() {
		return "cfg";
	}


	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
